﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace AgLux
{
    /// <summary>
    /// Represents a component that displays formatted text.
    /// </summary>

    public class Text : Component
    {
        protected TextBlock _text;

        /// <summary>
        /// Initializes a new instance of the Text class.
        /// </summary>

        /// <param name="content">
        /// The text content.
        /// </param>

        /// <param name="background">
        /// The text background
        /// </param>

        public Text(List<Inline> content, string background)
            : base()
        {
            _text = (TextBlock)_resource.FindName("text");

            _screen.SetValue(Canvas.BackgroundProperty, background);
            foreach (Inline part in content)
            {
                _text.Inlines.Add(part);
            }
        }

        /// <summary>
        /// Gets the text content as HTML.
        /// </summary>

        public string Html
        {
            get
            {
                string html = "";

                foreach (Inline content in _text.Inlines)
                {
                    if (content.GetType() == new Run().GetType())
                    {
                        Run part = (Run)content;

                        if (part.FontWeight == FontWeights.Bold)
                        {
                            html += "<b>";
                        }
                        if (part.FontStyle == FontStyles.Italic)
                        {
                            html += "<i>";
                        }
                        if (part.TextDecorations == TextDecorations.Underline)
                        {
                            html += "<u>";
                        }

                        html += "<font style=\"font-family:" + part.FontFamily + "; font-size:" + part.FontSize + "px;\">" + part.Text + "</font>";

                        if (part.FontWeight == FontWeights.Bold)
                        {
                            html += "</b>";
                        }
                        if (part.FontStyle == FontStyles.Italic)
                        {
                            html += "</i>";
                        }
                        if (part.TextDecorations == TextDecorations.Underline)
                        {
                            html += "</u>";
                        }
                    }
                    else
                    {
                        html += "<br/>";
                    }
                }

                return html;
            }
        }

        /// <summary>
        /// Prepare to display the component.
        /// </summary>

        /// <param name="width">
        /// The width of the canvas on which the component will be displayed.
        /// </param>

        public override void Prepare(double width)
        {
            Width = _screen.Width = _text.Width = width;
            Height = _screen.Height = _text.Height = _text.ActualHeight;
        }
    }
}
